package com.cn.yang.service.impl;


import com.cn.yang.dao.MainPageDao;
import com.cn.yang.dao.impl.MainPageDaoImpl;
import com.cn.yang.model.mainPage.HeadAndFootModel;
import com.cn.yang.model.mainPage.HeadLinkModel;
import com.cn.yang.model.mainPage.MainPageModel;
import com.cn.yang.service.MainPageService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class MainPageServiceImpl implements MainPageService {

    //头和尾的一些Model的查找和更新
    @Override
    public HeadAndFootModel selectHeadFoot() {
        MainPageDao dao = new MainPageDaoImpl();
        Jedis jedis = new Jedis();
        String headfoot_id = jedis.hget("headandfoot","id");
        HeadAndFootModel model = new HeadAndFootModel();
        if (headfoot_id != null){
            model.setId(Integer.valueOf(jedis.hget("headandfoot","id")));
            model.setTitle(jedis.hget("headandfoot","title"));
            model.setEnglishtitle(jedis.hget("headandfoot","englishname"));
            model.setPhoto(jedis.hget("headandfoot","photo"));
            model.setFoothref(jedis.hget("headandfoot","foothref"));
            model.setFootwords(jedis.hget("headandfoot","footwords"));
        }else {
            model =  dao.selectHeadFoot();
            jedis.hset("headandfoot","id",model.getId().toString());
            jedis.expire("headandfoot",10*60);
            jedis.hset("headandfoot","title",model.getTitle());
            jedis.hset("headandfoot","englishname",model.getEnglishtitle());
            jedis.hset("headandfoot","photo",model.getPhoto());
            jedis.hset("headandfoot","foothref",model.getFoothref());
            jedis.hset("headandfoot","footwords",model.getFootwords());
        }
        return model;
    }
//更新头尾数据
    @Override
    public int updataHeadFoot(HeadAndFootModel model) {
        MainPageDao dao = new MainPageDaoImpl();
        Jedis jedis = new Jedis();
        jedis.del("headandfoot");
        return dao.updataHeadFoot(model);
    }
//头部链接的=========================================================
    @Override
    public List<HeadLinkModel> selectHeadLink() {
        MainPageDao dao = new MainPageDaoImpl();
        List<HeadLinkModel> list = new ArrayList();
        Jedis jedis = new Jedis();
        long length = jedis.llen("headLink_id");
        if (length>0){
            List<String> headLink_id = jedis.lrange("headLink_id",0,-1);
            List<String> headLink_title = jedis.lrange("headLink_title",0,-1);
            List<String> headLink_href = jedis.lrange("headLink_href",0,-1);
            List<String> headLink_isshow = jedis.lrange("headLink_isshow",0,-1);
            for (int i=0;i<length;i++){
                HeadLinkModel model = new HeadLinkModel();
                model.setId(Integer.parseInt(headLink_id.get(i)));
                model.setTitle(headLink_title.get(i));
                model.setHref(headLink_href.get(i));
                model.setIsshow(Integer.parseInt(headLink_isshow.get(i)));
                list.add(model);
            }
        }else {
            list = dao.selectHeadLink();
            for (int i = 0;i<list.size();i++){
                jedis.rpush("headLink_id",list.get(i).getId().toString());
                jedis.expire("headLink_id",10*60);
                jedis.rpush("headLink_title",list.get(i).getTitle());
                jedis.expire("headLink_title",10*60);
                jedis.rpush("headLink_href",list.get(i).getHref());
                jedis.expire("headLink_href",10*60);
                jedis.rpush("headLink_isshow",list.get(i).getIsshow().toString());
                jedis.expire("headLink_isshow",10*60);
            }
        }
        return list;
    }
    //添加头部链接
    @Override
    public int addHeadLink(HeadLinkModel model) {
        MainPageDao dao = new MainPageDaoImpl();
        Jedis jedis = new Jedis();
        jedis.del("headLink_id");
        jedis.del("headLink_title");
        jedis.del("headLink_href");
        return dao.addHeadLink(model);
    }
//查询头部链接 通过ID查找
    @Override
    public HeadLinkModel selectHeadLinkById(Integer id) {
        MainPageDao dao = new MainPageDaoImpl();
        return dao.selectHeadLinkById(id);
    }
//更新头尾链接
    @Override
    public int updateById(HeadLinkModel model) {
        MainPageDao dao = new MainPageDaoImpl();
        Jedis jedis = new Jedis();
        jedis.del("headLink_id");
        jedis.del("headLink_title");
        jedis.del("headLink_href");
        return dao.updateById(model);
    }
//删除头尾链接
    @Override
    public int deleteHeadLink(String id) {
        MainPageDao dao = new MainPageDaoImpl();
        Jedis jedis = new Jedis();
        jedis.del("headLink_id");
        jedis.del("headLink_title");
        jedis.del("headLink_href");
        return dao.deleteHeadLink(id);
    }
//主页 分 model
    @Override
    public List<MainPageModel> findMainPage(MainPageModel model) {
        MainPageDao dao = new MainPageDaoImpl();
        Jedis jedis = new Jedis();
        List<MainPageModel> list = new ArrayList<>();
        long length = jedis.llen("mainPage_id");
        if (length>0){
            System.out.println("走的redis");
            List<String> mainPage_id = jedis.lrange("mainPage_id",0,-1);
            List<String> mainPage_title = jedis.lrange("mainPage_title",0,-1);
            List<String> mainPage_englishname = jedis.lrange("mainPage_englishname",0,-1);
            List<String> mainPage_isshow = jedis.lrange("mainPage_isshow",0,-1);
            for (int i =0;i<length;i++){
                MainPageModel model1 = new MainPageModel();
                model1.setId(Integer.parseInt(mainPage_id.get(i)));
                model1.setTitle(mainPage_title.get(i));
                model1.setEnglishName(mainPage_englishname.get(i));
                model1.setIsshow(mainPage_isshow.get(i));
                list.add(model1);
            }
        }else {
            list = dao.findMainPage(model);
            System.out.println("走的数据库");
            for (int i = 0;i<list.size();i++){
                jedis.rpush("mainPage_id",String.valueOf(list.get(i).getId()));
                jedis.expire("mainPage_id",10*60);
                jedis.rpush("mainPage_title",list.get(i).getTitle());
                jedis.expire("mainPage_title",10*60);
                jedis.rpush("mainPage_englishname",list.get(i).getEnglishName());
                jedis.expire("mainPage_englishname",10*60);
                jedis.rpush("mainPage_isshow",list.get(i).getIsshow());
                jedis.expire("mainPage_isshow",10*60);
            }
        }
        return list;
    }
    public List<MainPageModel> findMainPage() {
        MainPageDao dao = new MainPageDaoImpl();
        Jedis jedis = new Jedis();
        List<MainPageModel> list = new ArrayList<>();
        long length = jedis.llen("mainPage_id");
        if (length>0){
            List<String> mainPage_id = jedis.lrange("mainPage_id",0,-1);
            List<String> mainPage_title = jedis.lrange("mainPage_title",0,-1);
            List<String> mainPage_englishname = jedis.lrange("mainPage_englishname",0,-1);
            List<String> mainPage_isshow = jedis.lrange("mainPage_isshow",0,-1);
            for (int i =0;i<length;i++){
                MainPageModel model = new MainPageModel();
                model.setId(Integer.parseInt(mainPage_id.get(i)));
                model.setTitle(mainPage_title.get(i));
                model.setEnglishName(mainPage_englishname.get(i));
                model.setIsshow(mainPage_isshow.get(i));
                list.add(model);
            }
        }else {
            list = dao.findMainPage();
            System.out.println("走的数据库");
            for (int i = 0;i<list.size();i++){
                jedis.rpush("mainPage_id",String.valueOf(list.get(i).getId()));
                jedis.expire("mainPage_id",10*60);
                jedis.rpush("mainPage_title",list.get(i).getTitle());
                jedis.expire("mainPage_title",10*60);
                jedis.rpush("mainPage_englishname",list.get(i).getEnglishName());
                jedis.expire("mainPage_englishname",10*60);
                jedis.rpush("mainPage_isshow",list.get(i).getIsshow());
                jedis.expire("mainPage_isshow",10*60);
            }
        }
        return list;
    }
//总页计数
    @Override
    public int countMainPage() {
        MainPageDao dao = new MainPageDaoImpl();
        return dao.countMainPage();
    }
//查询通过ID 返回页面上的数据
    @Override
    public MainPageModel selectById(Integer id) {
        MainPageDao dao = new MainPageDaoImpl();
        return dao.selectById(id);
    }
//更新主页面的值
    @Override
    public int updateMainPage(MainPageModel model) {
        MainPageDao dao = new MainPageDaoImpl();
        System.out.println("你好");
        Jedis jedis = new Jedis();
        jedis.del("mainPage_id");
        jedis.del("mainPage_englishname");
        jedis.del("mainPage_isshow");
        jedis.del("mainPage_title");
        return dao.updateMainPage(model);
    }
    
    //更新 是否 启用
    @Override
    public void updateIsshowHeadLink(HeadLinkModel model) {
        MainPageDao dao = new MainPageDaoImpl();
        Jedis jedis = new Jedis();
        jedis.del("headLink_id");
        jedis.del("headLink_title");
        jedis.del("headLink_href");
        jedis.del("headLink_isshow");
        dao.updateIsshowHeadLink(model);
    }
}
